package server.models.terrain;

import java.io.Serializable;
import java.rmi.AccessException;
import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Observable;

import rmi.RemoteObserver;
import rmi.UtilitairesRmi;
import rmi.WrappedObserver;
import server.models.cabane.Cabane;
import server.models.cabane.ICabane;
import server.models.posteDeTir.IPosteDeTir;
import server.models.posteDeTir.PosteDeTir;

public class Terrain extends Observable implements ITerrain, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private ICabane cabaneGauche;
	private ICabane cabaneDroite;
	private ArrayList<IPosteDeTir> listePostesDeTir;
	
	public Terrain() throws AccessException, RemoteException, AlreadyBoundException, NotBoundException {
		this.listePostesDeTir = new ArrayList<IPosteDeTir>();
		this.cabaneGauche = (ICabane)UtilitairesRmi.getInstance().obtenirEtBind(new Cabane("Gauche"));
		this.cabaneDroite = (ICabane)UtilitairesRmi.getInstance().obtenirEtBind(new Cabane("Droite"));
	}
	
	public ArrayList<IPosteDeTir> getListePostesDeTir() throws RemoteException {
		return listePostesDeTir;
	}

	public void setListePostesDeTir(ArrayList<IPosteDeTir> listePostesDeTir) {
		this.listePostesDeTir = listePostesDeTir;
	}

	public ICabane getCabaneGauche() throws RemoteException {
		return cabaneGauche;
	}

	public void setCabaneGauche(ICabane cabaneGauche) {
		this.cabaneGauche = cabaneGauche;
	}

	public ICabane getCabaneDroite() throws RemoteException {
		return cabaneDroite;
	}

	public void setCabaneDroite(ICabane cabaneDroite) {
		this.cabaneDroite = cabaneDroite;
	}
	
	/**
	 * Ajouter un poste de tir
	 * @param posteDeTir	Le poste de tir ajouté
	 */
	public void ajouterPosteDeTir(IPosteDeTir posteDeTir) throws RemoteException {
		if (this.listePostesDeTir.size() < 8) {
			this.listePostesDeTir.add(posteDeTir);
		}
	}

	@Override
	public void addObserver(RemoteObserver remoteObservable) throws RemoteException {
		this.addObserver(new WrappedObserver(remoteObservable));		
	}
}
